import json
from boot import logger, CONFIG_ROOT_DIR


__all__ = [
    "get_json_config",
    "set_json_config"
]


def get_json_config(config_name: str) -> dict:
    """
    获取配置文件函数
    :param config_name: 配置文件名称
    :return: 配置文件内容
    """
    try:
        with open(CONFIG_ROOT_DIR + "/" + config_name + ".json", "r", encoding="utf-8") as f:
            return json.load(f)
    except Exception as e:
        logger.warning("读取配置文件失败：" + str(e))
        exit(-1)


def set_json_config(config_name: str, config_dict: dict) -> None:
    """
    写入配置文件函数
    :param config_name: 配置文件名称
    :param config_dict: 配置文件内容
    :return: None
    """
    try:
        with open(CONFIG_ROOT_DIR + "/" + config_name + ".json", "w", encoding="utf-8") as f:
            json.dump(config_dict, f, ensure_ascii=False, indent=4)
    except Exception as e:
        logger.warning("更新配置文件失败：" + str(e))